﻿using System;
using System.Windows;
using System.Windows.Media;
using AvalonDock;
using DataGenerator.DefaultSets;
using Dispatcher.Helpers;
using Dispatcher.ViewModels;
using Repository;

namespace Dispatcher.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            DockingManager = DockManager;
            ThemeFactory.ChangeColors(Colors.Silver);
            try
            {
                Model = new MainViewModel(DockManager);
                DataContext = Model;
                Instance = this;
                EventGroupTypeSet = new EventGroupTypeSet(Model.ModelContainer.EventGroupTypeSet);
            }
            catch (Exception)
            {
                MessageBox.Show("Can not connect to database.");
                Close();
            }
        }

        /// <summary>
        /// Gets or sets the current user.
        /// </summary>
        /// <value>
        /// The current user.
        /// </value>
        public static User CurrentUser { get; set; }

        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        /// <value>
        /// The model.
        /// </value>
        public static MainViewModel Model { get; set; }

        /// <summary>
        /// Gets the docking manager.
        /// </summary>
        public static DockingManager DockingManager { get; private set; }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static MainWindow Instance { get; private set; }

        /// <summary>
        /// Gets the event group type set.
        /// </summary>
        public static EventGroupTypeSet EventGroupTypeSet { get; private set; }

        /// <summary>
        /// Commands the executed.
        /// </summary>
        /// <param name="o">The o.</param>
        /// <param name="e">The <see cref="Dispatcher.Helpers.ExtendedEventArgs"/> instance containing the event data.</param>
        public static void CommandExecuted(object o, ExtendedEventArgs e)
        {
            if (e.HasException)
            {
                MessageBox.Show(e.Exception.Message, e.Args.Command.ToString());
            }
        }

        /// <summary>
        /// Handles the Loaded event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Model.OnLoad();
        }
    }
}
